const { User } = require("../model");
const { Model } = require("../model");
const jwt = require("../util/jwt");
const { jwtSecret } = require("../config/config.default");
// 用户登录
exports.login = async (req, res, next) => {
  try {
    const user = req.user.toJSON();
    const token = await jwt.sign(
      {
        id: user._id,
      },
      jwtSecret,
      {
        expiresIn: "7d", // 设置过期时间 单位 秒
        /**
         * 30 30s
         * 2 days
         * 10h
         * 7d
         */
      }
    );
    res.status(200).json({
      code: 200,
      msg: "success",
      token,
      user,
    });
  } catch (error) {
    next(error);
  }
};

// 用户注册
exports.register = async (req, res, next) => {
  try {
    const user = new User(req.body);
    await user.save();
    res.status(200).json({
      code: 200,
      msg: "success",
    });
  } catch (error) {
    next(error);
  }
};

// 编辑用户
exports.updateUser = async (req, res, next) => {
  try {
    const model = await User.findByIdAndUpdate(req.params.id, {
      $set: req.body
    })
    if (model) {
      res.status(200).json({
        code: 200,
        msg: "success",
      });
    } else {
      res.status(200).json({
        code: 500,
        msg: "修改失败",
      });
    }
    
  } catch (error) {
    next(error);
  }
};

exports.getAllUsers = async (req, res, next) => {
  try {
    const items = await User.find();
    res.status(200).json({
      code: 200,
      data: items,
    });
  } catch (error) {
    next(error);
  }
};
exports.getModel = async (req, res, next) => {
  try {
    const items = await Model.find();
    res.status(200).json({
      code: 200,
      data: items,
    });
  } catch (error) {
    next(error);
  }
};
exports.addModel = async (req, res, next) => {
  try {
    const model = new Model(req.body);
    await model.save();
    res.status(200).json({
      code: 200,
      msg: "success",
    });
  } catch (error) {
    next(error);
  }
};
exports.getUser = async (req, res, next) => {
  try {
    const item = await User.findById(req.params.id);
    res.status(200).json({
      code: 200,
      data: item,
    });
  } catch (error) {
    next(error);
  }
};



exports.deleteUser = async (req, res, next) => {
  try {
    const rs = await User.findByIdAndDelete(req.params.id)
    if (rs) {
      res.status(200).json({
        code: 200,
        msg: '删除成功'
      });
    }
  } catch (error) {
    next(error);
  }
};

exports.deleteModel = async (req, res, next) => {
  try {
    const rs = await Model.findByIdAndDelete(req.params.id)
    if (rs) {
      res.status(200).json({
        code: 200,
        msg: '删除成功'
      });
    }
  } catch (error) {
    next(error);
  }
};

// 获取当前用户
exports.getCurrentUser = async (req, res, next) => {
  try {
    const user = req.user.toJSON();
    delete user.password;
    res.status(200).json({
      code: 200,
      msg: "success",
      data: user,
    });
  } catch (error) {
    next(error);
  }
};

// 更新用户
exports.updateUserData = async (req, res, next) => {
  try {
    const user = await User.findByIdAndUpdate(req.params.id, {
      data: req.body.data,
    });
    res.status(200).json({
      code: 200,
      msg: "success",
    });
  } catch (error) {
    next(error);
  }
};

// 更新用户
exports.updateModelData = async (req, res, next) => {
  try {
    await Model.findByIdAndUpdate(req.params.id, req.body);
    res.status(200).json({
      code: 200,
      msg: "success",
    });
  } catch (error) {
    next(error);
  }
};

// 是否有超管
exports.hasAdmin = async (req, res, next) => {
  try {
    const admin = await User.find({
      username: 'admin'
    });
    if (!admin || !admin.length) {
      // 新增超管用户
      const user = new User(req.body);
      await user.save();
      res.status(200).json({
        code: 200,
        msg: "success",
      });
    } else {
      res.status(200).json({
        code: 200,
        msg: "success",
      });
    }
  } catch (error) {
    next(error);
  }
};
